package com.aivin.lib_widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * 九宫格控件 ，
 * 最多只支持九张图片
 */
public class NineGridLayout extends ViewGroup {
    private Context mContext;
    /**
     *  图片之间的间隔
     */
    private float mSpacing= 20f ;

    /**
     * 容器边框的距离
     */
    int paddingLeft = 10 ;
    int paddingRight =10;
    int paddingTop =10 ;
    int paddingBottom =10 ;

    /**
     * 列数
     */
    private int mColumns;
    /**
     * 行数
     */
    private int mRows;
    /**
     * 图片的宽 (高跟宽一样)
     */
    private int mSingleWidth;

    private boolean mIsFirst = true;
    private List<Integer> mUrlList = new ArrayList<>();

    public NineGridLayout(Context context) {
        this(context, null);
    }

    public NineGridLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }


    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        int mTotalWidth = right - left;
        mSingleWidth = (int) ((mTotalWidth - mSpacing * 2 -paddingLeft - paddingRight) / 3);

        if (mIsFirst) {
            notifyDataSetChanged();
            mIsFirst = false;
        }
    }


    private void notifyDataSetChanged() {
        removeAllViews();
        int size = mUrlList==null ? 0: mUrlList.size()  ;
        generateChildrenLayout(size);
        layoutParams();

        for (int i = 0; i < size; i++) {
            int resourceId = mUrlList.get(i);
            ImageView imageView = createImageView(i );
            layoutImageView(imageView, i, resourceId);
        }
    }



    private ImageView createImageView(final int i ) {
        final ImageView imageView = new MyColorFilterImageView(mContext);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

        imageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickImage(i   );
            }
        });

        return imageView;
    }

    private void layoutParams() {
        LayoutParams params = getLayoutParams();
        params.height = (int) ( mSingleWidth * mRows + mSpacing * (mRows-1)  + paddingTop + paddingBottom );
        setLayoutParams(params);
    }




    private void layoutImageView(ImageView imageView, int i, int resourceId) {
        int[] position = findPosition(i); //  行 列

        int left = (int) ((mSingleWidth + mSpacing) * position[1]) + paddingLeft;
        int top = (int) ((mSingleWidth + mSpacing) * position[0]) + paddingTop;


        int right = left + mSingleWidth;
        int bottom = top + mSingleWidth;

        imageView.layout(left, top, right, bottom);
        addView(imageView);
        displayImage( imageView, resourceId);
    }

    private int[] findPosition(int childNum) {
        int[] position = new int[2];
        for (int i = 0; i < mRows; i++) {
            for (int j = 0; j < mColumns; j++) {
                if ((i * mColumns + j) == childNum) {
                    position[0] = i;//行
                    position[1] = j;//列
                    break;
                }
            }
        }
        return position;
    }


    private void generateChildrenLayout(int size) {
        if (size <= 3) {
            mRows = 1;
            mColumns = size;
        }

        else if (size <= 6) {
            mRows = 2;
            mColumns = 3;
        }

        else {
            mColumns = 3;
            mRows = 3;
        }
    }



    private void displayImage(ImageView imageView, int resourceId) {
        Picasso.with(mContext).load(resourceId).error(mContext.getResources().getDrawable(R.mipmap.load_error)).into(imageView);
    }

    private void onClickImage(int imageIndex  ) {
        if(onNiewGridLayoutItemClickListner!=null){
            onNiewGridLayoutItemClickListner.onItemClick(imageIndex);
        }

    }



    public void setUrlList(List<Integer> urlList) {
        mUrlList.clear();
        mUrlList.addAll(urlList);
    }

    public void setOnNiewGridLayoutItemClickListner(OnNiewGridLayoutItemClickListner onNiewGridLayoutItemClickListner) {
        this.onNiewGridLayoutItemClickListner = onNiewGridLayoutItemClickListner;
    }

    private OnNiewGridLayoutItemClickListner onNiewGridLayoutItemClickListner ;
    public interface OnNiewGridLayoutItemClickListner {
        void onItemClick(int index);
    }

}
